// plugins/my-plugin/assets/javascripts/discourse/components/share-link-component.js.es6

import Component from '@ember/component';
import { inject as service } from '@ember/service';
import { computed } from '@ember/object';

export default Component.extend({
    router: service(),
    currentUser: service(),

    // 获取当前分享链接，并且
    shareLink: computed('router.currentURL', function () {
        const baseUrl = window.location.origin;
        const currentPath = this.router.currentURL;
        return `${baseUrl}${currentPath}`;
    }),

    // 获取当前用户名
    username: computed('currentUser.username', function () {
        return this.currentUser.username || 'Guest';
    }),

    actions: {
        // 点击事件处理函数
        handleShare(source) {
            const username = this.get('username');
            let shareLink = this.get('shareLink') + `?u=${username}`;
            // 获取当前链接中相关图片
            const img = "https://bbs.openkylin.top/uploads/default/original/3X/4/e/4e1d0f6dfd71823432e44b2656bef4eaafcac8a9.png";
            
            switch (source) {
                case ('weibo'):
                    window.open(`http://service.weibo.com/share/share.php?url=${shareLink}&title=${document.title}&pic=&appkey=&searchPic=false`);
                    break;
                case ('qq'):
                    window.open(`http://connect.qq.com/widget/shareqq/index.html?url=${shareLink}&title=${document.title}&pics=${img}&source=openKylin&desc=&summary=&site=${window.location.origin}`);
                    break;
            }
        }
    }
});